package cn.tedu.medicalmanagementsystem.escort.service;

import cn.tedu.medicalmanagementsystem.base.exception.BusinessException;
import cn.tedu.medicalmanagementsystem.escort.pojo.dto.ReviewReplyParam;
import cn.tedu.medicalmanagementsystem.user.mapper.ReviewMapper;
import cn.tedu.medicalmanagementsystem.user.pojo.vo.ReviewVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class ReviewApplyService {
    private final ReviewMapper reviewMapper;

    /**
     * 查询陪诊员的所有评价
     */
    public List<ReviewVO> getReviewsByEscort(Long escortId) {
        return reviewMapper.selectByEscortId(escortId);
    }

    /**
     * 回复评价（增加权限校验）
     */
    public void replyReview(ReviewReplyParam param, Long escortId) {
        // 校验评价是否属于当前陪诊员
        if (!reviewMapper.existsByIdAndEscortId(param.getReviewId(), escortId)) {
            throw new BusinessException("无权回复该评价");
        }

        int num = reviewMapper.updateReply(param.getReviewId(), escortId, param.getReplyContent());
        if (num == 0) {
            throw new BusinessException("回复失败，请稍后重试");
        }
    }
}